#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from email_validator import EmailNotValidError, validate_email
from pydantic import ConfigDict, field_validator, Field

from backend.app.schemas.base import SchemaBase


class Auth(SchemaBase):
    mobile: str
    password: str


class Auth2(Auth):
    captcha_code: str


class CreateUser(SchemaBase):
    username: str
    password: str
    email: str

    @field_validator('email')
    @classmethod
    def email_validate(cls, v: str):
        try:
            validate_email(v, check_deliverability=False).email
        except EmailNotValidError:
            raise ValueError()
        return v


class UpdateUser(SchemaBase):
    username: str
    email: str
    phone: str | None = None

    @field_validator('email')
    @classmethod
    def email_validate(cls, v: str):
        try:
            validate_email(v, check_deliverability=False).email
        except EmailNotValidError:
            raise ValueError('邮箱格式错误')
        return v


class GetUserInfo(SchemaBase):
    model_config = ConfigDict(from_attributes=True)

    id: int


class ResetPassword(SchemaBase):
    code: str
    password1: str
    password2: str


class UserAuthSchema(SchemaBase):
    uid: int = Field(description="用户ID")
    shop_code: str = Field(description="商户编号", alias="shopCode")
    uid_type: int = Field(description="用户类型(3为平台，4为供应商，5为人才 0为非后台默认0)", alias="uidType")
    shop_id: int = Field(description="商户ID", alias="shopId")
    shop_key: int | str = Field(description="商户编码", alias="shopKey")
    platform: str = Field(description="搜索类型", default="avl")
